package jp.sourceforge.akjrcp.flvtools.basic;

import java.io.File;
import java.util.ArrayList;

import jp.sourceforge.akjrcp.ui.DirectoryList;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Table;
public class FLVDirectoryList extends DirectoryList{

	public FLVDirectoryList(Composite parent,int style){
		super(parent,style);
		setLabelProvider(new ViewLabelProvider());
	}


	protected static  Image questionImage =new Image(Display.getCurrent(), DirectoryList.class.getClassLoader()
            .getResourceAsStream(
            "jp/sourceforge/akjrcp/flvtools/basic/resource/question.png"));;
            protected static Image flvImage =new Image(Display.getCurrent(), DirectoryList.class.getClassLoader()
            .getResourceAsStream(
            "jp/sourceforge/akjrcp/flvtools/basic/resource/flv.png"));;
            



	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return ((File)obj).getName();
		}
		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}
		public Image getImage(Object obj) {
			File file=(File)obj;
			if(file.isDirectory()){
				return DirectoryList.folderImage;
			}else{
				if(isFLVExtension(file.getName())){
					return flvImage;
				}else{
					return questionImage;
				}
			}
		}
	}
	
	private boolean isFLVExtension(String name){
		return name.toLowerCase().endsWith(".flv");
	}
	public void setlectAllUnknown(){
	File files[]=getFileList();
	ArrayList selectList=new ArrayList();
	for(int i=0;i<files.length;i++){
		if(!isFLVExtension(files[i].getName())){
			selectList.add(new Integer(i));
		}
	}
	Integer[] integers=(Integer[]) selectList.toArray(new Integer[0]);
	int[] ints=new int[integers.length];
	
	for (int i = 0; i < ints.length; i++) {
		ints[i]=integers[i].intValue();
	}
	((Table)this.getControl()).setSelection(ints);
	}
	
	public void updateDirectory(){
		SimpleView.log.info("updateDirectory");
		fileList.removeAll(fileList);
		String list[]=null;
		if(filenameFilter!=null){
			list=baseDirectory.list(filenameFilter);
		}else{
			list=baseDirectory.list();
			if(list==null){
				list=new String[0];
			}
		}
		
		
		ArrayList flvList=new ArrayList();
		ArrayList unknownList=new ArrayList();
		//add
		for (int i = 0; i < list.length; i++) {
			
				if(list[i].toLowerCase().endsWith(".flv")){
					flvList.add(new File(baseDirectory,list[i]));
				}else{
					unknownList.add(new File(baseDirectory,list[i]));
				}
				
			
		}
		File[] unknownFiles=(File[]) unknownList.toArray(new File[0]);
		for (int i = 0; i < unknownFiles.length; i++) {
			addFile(unknownFiles[i]);
		}
		
		File[] flvFiles=(File[]) flvList.toArray(new File[0]);
		for (int i = 0; i < flvFiles.length; i++) {
			addFile(flvFiles[i]);
		}
		this.refresh();
	}

}
